package androidgame.teslawars;



import javax.microedition.khronos.opengles.GL10;

import androidgame.framework.gl.Camera2D;
import androidgame.framework.gl.SpriteBatcher;
import androidgame.framework.impl.GLGraphics;
import androidgame.teslawars.game.Level;
import androidgame.teslawars.game.Tile;
import androidgame.teslawars.game.Unit;

// @author dicarlo2

public class WorldRenderer {
	GLGraphics glGraphics;
	Level level;
	Camera2D worldCam;
	SpriteBatcher batcher;

	public WorldRenderer(GLGraphics glGraphics, SpriteBatcher batcher,
			Level level, Camera2D worldCam) {
		this.glGraphics = glGraphics;
		this.level = level;
		this.worldCam = worldCam;
		this.batcher = batcher;
	}

	public void render() {
		// Update where the camera is here from the World.
		worldCam.ensureWithinScene(level.getWorldWidth(), level.getWorldHeight(), .5f, .5f);
		worldCam.setViewportAndMatrices();
		renderMap();
	}

	public void renderMap() {
		batcher.beginBatch(Assets.items);
		int len1 = level.getWorldWidth();
		int len2 = level.getWorldHeight();
		for (int i = 0; i < len1 ; i++) {
			for (int j = 0; j < len2; j++) {
				switch (level.getGrid()[i][j].getTerrain()){
				case PLAIN:
					batcher.drawSprite(i + .5f, j + .5f, 1.0f, 1.0f, Assets.grass1);
					break;
				case FOREST:
					batcher.drawSprite(i + .5f, j + .5f, 1.0f, 1.0f, Assets.twoTrees);
					break;
				case MOUNTAIN:
					batcher.drawSprite(i + .5f, j + .5f, 1.0f, 1.0f, Assets.mountain);
					break;
				}
			}
		}	
		batcher.endBatch();
		
		GL10 gl = glGraphics.getGL();
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		batcher.beginBatch(Assets.items);
		for (Tile tile : level.getHighlightedTiles())
			batcher.drawSprite(tile.getX() + .5f, tile.getY() + .5f, 1.0f, 1.0f, Assets.highlight);
		for (Unit unit : level.getPlayer1UnitList()){
			switch (unit.getUnitName()){
			case TANK:
				batcher.drawSprite(unit.getX() + .5f, unit.getY() + .5f, 1.0f, 1.0f, Assets.player1Tank);
				break;
			case JET:
				batcher.drawSprite(unit.getX() + .5f, unit.getY() + .5f, 1.0f, 1.0f, Assets.player1Jet);
				break;
			case FOOTSOLDIER:
				batcher.drawSprite(unit.getX() + .5f, unit.getY() + .5f, 1.0f, 1.0f, Assets.player1Soldier);
				break;
			}
			if (unit.getHealth() == 10)
				Assets.readableFont.drawText(batcher, "" + unit.getHealth(), unit.getX() + .65f, unit.getY() + .1f, .25f, .25f);
			else
				Assets.readableFont.drawText(batcher, "" + unit.getHealth(), unit.getX() + .9f, unit.getY() + .1f, .25f, .25f);
		}
		for (Unit unit : level.getPlayer2UnitList()){
			switch (unit.getUnitName()) {
			case TANK:
				batcher.drawSprite(unit.getX() + .5f, unit.getY() + .5f, 1.0f, 1.0f, Assets.player2Tank);
				break;
			case JET:
				batcher.drawSprite(unit.getX() + .5f, unit.getY() + .5f, 1.0f, 1.0f, Assets.player2Jet);
				break;
			case FOOTSOLDIER:
				batcher.drawSprite(unit.getX() + .5f, unit.getY() + .5f, 1.0f, 1.0f, Assets.player2Soldier);
				break;
			}
			if (unit.getHealth() == 10)
				Assets.readableFont.drawText(batcher, "" + unit.getHealth(), unit.getX() + .65f, unit.getY() + .1f, .25f, .25f);
			else
				Assets.readableFont.drawText(batcher, "" + unit.getHealth(), unit.getX() + .9f, unit.getY() + .1f, .25f, .25f);
		}
		batcher.endBatch();
		gl.glDisable(GL10.GL_BLEND);
	}
	
}
